package com.example.springsecurity.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springsecurity.domain.College;
import com.example.springsecurity.domain.Major;
import com.example.springsecurity.domain.TbClass;
import com.example.springsecurity.service.CollegeService;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.ifnull.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName GroupManageController
 * @Description TODO     分组管理
 * @Author lvyongwei
 * @Date 2020-10-22 9:41
 **/
@Controller
@RequestMapping("/groupManage")
public class GroupManageController {

    @Autowired
    private CollegeService collegeService;


    /**
     * 获取学院信息
     */
    @PostMapping("/getColleges")
    @ResponseBody
    public ResultBean getColleges(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        if(StringUtils.isNull(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isNull(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        try{
            /**
             * 分页
             */
            PageHelper.startPage(pageNum,pageSize);
            List<College> collegeList = collegeService.getColleges();
            for(College college:collegeList){
                college.setMajorList((List<Major>) collegeService.getMajor(college.getCollegeId()).get("data"));
            }
            PageInfo<College> pageInfo = new PageInfo<College>(collegeList);
            return ResultBean.success("获取数据成功",pageInfo);
        }catch (Exception e){
            return ResultBean.error("获取数据失败");
        }
    }

    /**
     * 获取学院信息
     */
    @PostMapping("/getTbClasses")
    @ResponseBody
    public ResultBean getTbClasses(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        if(StringUtils.isNull(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isNull(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        try{
            /**
             * 分页
             */
            PageHelper.startPage(pageNum,pageSize);
            List<TbClass> tbClasses =  collegeService.getTbClasses();
            PageInfo<TbClass> pageInfo = new PageInfo<TbClass>(tbClasses);
            return ResultBean.success("获取数据成功",pageInfo);
        }catch (Exception e){
            return ResultBean.error("获取数据失败");
        }
    }

    /**
     * 添加学院信息
     *
     * @param college
     * @return
     */
    @PostMapping("/insertCollege")
    @ResponseBody
    public ResultBean insertCollege(@RequestBody College college) {
        try {
            collegeService.insertCollege(college);
            return ResultBean.success("添加学院成功");
        } catch (Exception e) {
            return ResultBean.error("添加学院失败");
        }
    }

    /**
     * 修改学院信息
     *
     * @param college
     * @return
     */
    @PostMapping("/updateCollege")
    @ResponseBody
    public ResultBean updateCollege(@RequestBody College college) {
        try {
            collegeService.updateCollege(college);
            return ResultBean.success("修改学院成功");
        } catch (Exception e) {
            return ResultBean.error("修改学院失败");
        }
    }

    /**
     * 添加专业信息
     * @param major
     * @return
     */
    @PostMapping("/insertMajor")
    @ResponseBody
    public ResultBean insertMajor(@RequestBody Major major) {
        try {
            collegeService.insertMajor(major);
            return ResultBean.success("添加专业成功");
        } catch (Exception e) {
            return ResultBean.error("添加专业失败");
        }
    }

    /**
     * 修改专业信息
     * @param major
     * @return
     */
    @PostMapping("/updateMajor")
    @ResponseBody
    public ResultBean updateMajor(@RequestBody Major major) {
        try {
            collegeService.updateMajor(major);
            return ResultBean.success("修改专业成功");
        } catch (Exception e) {
            return ResultBean.error("修改专业失败");
        }
    }

    /**
     * 添加班级信息
     * @param tbClass
     * @return
     */
    @PostMapping("/insertTbClass")
    @ResponseBody
    public ResultBean insertTbClass(@RequestBody TbClass tbClass) {
        try {
            collegeService.insertClass(tbClass);
            return ResultBean.success("添加班级成功");
        } catch (Exception e) {
            return ResultBean.error("添加班级失败");
        }
    }

    /**
     * 修改班级信息
     * @param tbClass
     * @return
     */
    @PostMapping("/updateTbClass")
    @ResponseBody
    public ResultBean updateTbClass(@RequestBody TbClass tbClass) {
        try {
            collegeService.updateClass(tbClass);
            return ResultBean.success("修改班级成功");
        } catch (Exception e) {
            return ResultBean.error("修改班级失败");
        }
    }


}
